package io.renren.common.utils;

import com.alibaba.fastjson.JSON;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.Random;
import java.util.UUID;

/**
 * @Auther: miaoguoxin
 * @Date: 2019/7/20 22:16
 * @Description:
 */
public class CommonUtil {
    /**
     * 生成随机字符串
     * @param length
     * @return
     */
    public static String getRandomString(int length){
        String str="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
        Random random=new Random();
        StringBuilder sb=new StringBuilder();
        for(int i=0;i<length;i++){
            int number=random.nextInt(62);
            sb.append(str.charAt(number));
        }
        return sb.toString();
    }

    /**
     * @Description: 获取客户端IP地址
     */
    public static String getIpAddr(HttpServletRequest request) {
        if (request==null){
            return "127.0.0.1";
        }
        String ip = request.getHeader("x-forwarded-for");
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }
        return "0:0:0:0:0:0:0:1".equals(ip) ? "127.0.0.1" : ip;
    }

    /**
     * 生成8位随机码（数字0~9+大写字母A~Z）
     *
     */
    public static String getRandom() {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < 8; i++) {
            int a = Math.abs((new Random()).nextInt(32));
            if (a <= 9) {
                sb.append((char) (a + 48));
            } else if (a < 33) {
                if ((a + 55) == 79 || (a + 55) == 73) {
                    sb.append((char) (a + 63));
                } else {
                    sb.append((char) (a + 55));
                }
            }
        }
        return sb.toString();
    }

    /**
     * 功能描述:获取随机字符串
     *
     * @param:
     * @return:
     * @auther: miaoguoxin
     * @date: 2019/2/25 0025 17:34
     */
    public static String getRandomString(int length, String string) {
        if (length <= 0 || StringUtils.isEmpty(string)) {
            return null;
        }
        StringBuilder sb = new StringBuilder();
        int len = string.length();
        for (int i = 0; i < length; i++) {
            int round = (int) Math.round(Math.random() * (len - 1));
            sb.append(string.charAt(round));
        }
        return sb.toString();
    }

    /**
     * @return UUID随机数
     */
    public static String getuuid(){

        return StringUtils.replace(UUID.randomUUID().toString(), "-", "");
    }

    public static String captcha() {
        return String.valueOf((int) ((Math.random() * ((1 << 3) + 1) + 1) * 1000));
    }




    /**
    * @Description: 生成随机区间数字
    * @Param:
    * @return:
    * @Author: miaoguoxin
    * @Date: 2019/11/8
    */
    public static int randomInt(int min,int max){
        return min+(int)(Math.random()*(max+1-min));
    }

}
